package com.newtouch.cq.common.web.service.impl;//package cn.com.mcc.base.bus.common.service.impl;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.newtouch.cq.common.web.service.ICommonService;
import com.newtouch.cq.lang.cache.CacheAble;
import com.newtouch.cq.lang.config.CacheProperties;
import com.newtouch.cq.lang.util.ServletUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@Service
public class CommonServiceImpl  implements ICommonService {

    @Resource
    private CacheAble cacheAble;

    @Override
    public void pictCode(String randomKey) {
        HttpServletResponse response = ServletUtils.getResponse();
        try (OutputStream out = response.getOutputStream();) {
            response.setContentType("image/png");
            //定义图形验证码的长、宽、验证码字符数、干扰线宽度
//            ShearCaptcha lineCaptcha = CaptchaUtil.createShearCaptcha(StateConstant.WIDTH, StateConstant.HEIGTH, StateConstant.CODECOUONT, StateConstant.THICKNESS);
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 5, 200);
            //验证码
            String code = lineCaptcha.getCode();
            //存入redis里
            cacheAble.put(CacheProperties.imageCodePrifix+randomKey,code, CacheProperties.imageCodeTimeout);
            lineCaptcha.write(out);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
